class UrlVersion < ActiveRecord::Base
  class << self
    def unique_url_for(str)
      formatted_str = urlify(str)
      if Easel::Config.reserved_urls.include?(formatted_str)
        unique_url_for("#{formatted_str}-page")
      end
      if count(:conditions => ["url = ?", formatted_str]) > 0 
        u = UrlVersion.find_by_url(formatted_str)
        u.version_number += 1
        u.save
      else
        u = UrlVersion.create(:url => formatted_str)
      end
      ((u.version_number == 1) ? formatted_str : (formatted_str + "-" + u.version_number.to_s))
    end

    def urlify(str)
      str.downcase.gsub(/ /,"-").gsub(/-&-/,"-and-")
    end # urlify
  end
end
